package tool.zyk.serializable;

import com.alibaba.fastjson2.JSON;
import tool.zyk.consts.SerializableTypeEnum;
import tool.zyk.stardand.RpcRequest;
import tool.zyk.stardand.RpcResponse;
import tool.zyk.utils.Asserts;
import tool.zyk.utils.ReflectUtils;

import java.lang.reflect.Method;

/**
 * author: zyk
 * datetime: 2024/2/3 22:42
 * describe:
 */
public class SerializableFactory {

    public static SerializableConverter getSerializableConverter(SerializableTypeEnum serializableTypeEnum) {
        switch (serializableTypeEnum) {
            case JSON:
                return getJson();
            case XML:
                return getXml();
            default:
                throw new UnsupportedOperationException("暂不支持的序列化类型" + serializableTypeEnum);
        }
    }

    public static SerializableConverter getSerializableConverter(Integer serializableType) {
        SerializableTypeEnum serializableTypeEnum = SerializableTypeEnum.findByCode(serializableType);
        return getSerializableConverter(serializableTypeEnum);

    }

    private static SerializableConverter getXml() {
        return new SerializableConverter() {
            @Override
            public byte[] parseTransferData2Object(Object object) {
                return null;
            }

            @Override
            public <T> T serialObj2Transfer(byte[] bytes, Class<T> tClass) {
                return null;
            }

            @Override
            public RpcRequest serialRpcRequest(byte[] bytes) {
                return null;
            }

            @Override
            public RpcResponse serialRpcResponse(byte[] bytes) {
                return null;
            }
        };
    }

    private static SerializableConverter getJson() {
        return new SerializableConverter() {
            @Override
            public byte[] parseTransferData2Object(Object object) {
                return JSON.toJSONBytes(object);
            }

            @Override
            public <T> T serialObj2Transfer(byte[] bytes, Class<T> tClass) {
                Asserts.isNotNull(bytes, "解析数据为空");
                Asserts.isNotNull(tClass, "解析返回值对象类型为空");
                return JSON.parseObject(new String(bytes), tClass);
            }

            @Override
            public RpcRequest serialRpcRequest(byte[] bytes) {
                RpcRequest rpcRequest = serialObj2Transfer(bytes, RpcRequest.class);
                Method method = ReflectUtils.getMethod(rpcRequest.getBeUseClass(), rpcRequest.getMethodCode());
                Object[] args = rpcRequest.getArgs();
                for (int i = 0; i < method.getParameterTypes().length; i++) {
                    Class<?> parameterType = method.getParameterTypes()[i];
                    Object o = JSON.parseObject(JSON.toJSONString(args[i]), parameterType);
                    args[i] = o;
                }
                return rpcRequest;
            }

            @Override
            public RpcResponse serialRpcResponse(byte[] bytes) {
                RpcResponse rpcResponse = serialObj2Transfer(bytes, RpcResponse.class);
                byte[] dataByte = JSON.parseObject(JSON.toJSONString(rpcResponse.getData()), byte[].class);
                Object o = JSON.parseObject(dataByte, ReflectUtils.getClassType(rpcResponse.getDataClass()));
                rpcResponse.setData(o);
                return rpcResponse;
            }
        };
    }
}
